#!/usr/bin/env python3
# -*- coding:utf-8 -*-
## author : mike
## date   : 2014.12.10
"""
"""

import os
import sys
from os.path import realpath, dirname, basename, isdir
sys.path.append(dirname(dirname(realpath(__file__))))
from xbc3.fileio import FileIO

class CountReferer(object):
    """docstring for CountDifferentU"""
    def __init__(self,*inputfiles):
        super(CountReferer, self).__init__()
        self.files_name = []
        for fn in inputfiles:
            if isdir(fn):
                continue
            if 'sys_ss' in  basename(fn):
                self.files_name.append(fn)
        if not self.files_name:
            print('Error:can not find sys_ss files!!!\n', file=sys.stderr)
            sys.exit()
        self.referer_dict={}

    def cookie_referer(self, filename):
        """"""
        print(filename,"\n")
        file = FileIO(filename)
        for line in file:  
            try:
                referer=line[line.find('|referer:')+9:line.find('|l:')]
                referer2=referer.split('?')[0]
                
                if referer2 in self.referer_dict:
                    self.referer_dict[referer2][0]+=1
                else:
                    self.referer_dict[referer2]=[1,referer]
                    
            except Exception as e:
                pass
#             print(self.referer_dict)

    def run(self):
        """"""
        for fn in self.files_name:
            self.cookie_referer(fn)        
        self.output()

    def output(self):
        """"""
        fpath=os.path.split(self.files_name[0])[0]
        result_path=os.path.join(fpath,'referer 去参——带参.csv')
        
        with FileIO(result_path,'w') as f:
            for k in sorted(self.referer_dict, key=lambda x:self.referer_dict[x][0], reverse=True):
                v = self.referer_dict[k]
                f.write('%d, %s ,%s\n' % (v[0], k, v[1]))
                


if __name__ == '__main__':
    '''driver'''
    inputfiles = []
    for line in sys.stdin:
        line = line.rstrip('\r\n')
        inputfiles.append(line)
    print(len(inputfiles))
    cr=CountReferer(*inputfiles)
    cr.run()